﻿/*
 * date：2023/4/24
 * subject：上传文件帮助类
 * author：max
 * 
 * 
 * **/
using Microsoft.AspNetCore.Http;
using System.Collections;

namespace Max.PH5_2010A.Shopping.Common
{
    /// <summary>
    /// 上传文件帮助类
    /// </summary>
    public class FileUploadHelper
    {
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="files">文件对象</param>
        /// <param name="directory">文件保存目录</param>
        /// <param name="validSize">是否验证文件大小</param>
        /// <param name="size">默认1M（单位M）</param>
        /// <param name="validExt">验证文件格式</param>
        /// <param name="exts">文件格式数组</param>
        public static async Task<Tuple<bool, string, List<string>>> Upload(IFormFileCollection files, string directory = "temp",
            bool validSize = false, int size = 1,
            bool validExt = false, string[] exts = null)
        {

            List<string> result = null;
            try
            {
                if (files == null || files.Count() <= 0)
                {
                    return Tuple.Create(false, "请选择文件", result);
                }

                result = new List<string>();
                foreach (var item in files)
                {
                    #region 验证文件大小

                    if (validSize)
                    {
                        if (item.Length > size * 1024 * 1024)
                        {
                            return Tuple.Create(false, $"文件【{item.FileName}】超过限制", result);
                        }
                    }
                    #endregion

                    #region 验证文件格式
                    var _ext = System.IO.Path.GetExtension(item.FileName);
                    if (validExt)
                    {
                        if (!((IList)exts).Contains(_ext))
                        {
                            return Tuple.Create(false, $"文件格式不正确", result);
                        }
                    }
                    #endregion

                    #region 对文件重命名
                    string newFileName = Guid.NewGuid().ToString("N");
                    newFileName = $"{newFileName}{_ext}";
                    #endregion

                    #region 文件保存路径
                    if (!System.IO.Directory.Exists(directory))
                    {
                        System.IO.Directory.CreateDirectory(directory);
                    }
                    string filePath = System.IO.Path.Combine(directory, newFileName);
                    #endregion

                    #region 上传文件

                    using (var stream = File.OpenWrite(filePath))
                    {
                        await item.CopyToAsync(stream);
                        result.Add(newFileName);
                    }

                    #endregion
                }

                return Tuple.Create(true, "上传成功", result);
            }
            catch (Exception ex)
            {

                return  Tuple.Create(false, ex.Message, result); 
            }

        }
    }
}